package org.tis.tools.operatelog.module.log.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.tis.tools.model.exception.i18.ExceptionCodes;
import org.tis.tools.model.log.LogData;
import org.tis.tools.model.log.LogDataChange;
import org.tis.tools.model.log.LogOperate;
import org.tis.tools.model.log.vo.LogDataDetail;
import org.tis.tools.model.log.vo.LogOperateDetail;
import org.tis.tools.operatelog.module.log.dao.LogOperateMapper;
import org.tis.tools.operatelog.module.log.exception.OperateLogException;
import org.tis.tools.operatelog.module.log.service.ILogDataChangeService;
import org.tis.tools.operatelog.module.log.service.ILogDataService;
import org.tis.tools.operatelog.module.log.service.ILogOperateService;

import java.util.Date;

import static org.tis.tools.core.utils.BasicUtil.wrap;

/**
 * describe:
 *
 * @author zhaoch
 * @date 2018/3/29
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class LogOperateServiceImpl extends ServiceImpl<LogOperateMapper, LogOperate>
        implements ILogOperateService {

    @Autowired
    private ILogDataChangeService logChangeService;
    @Autowired
    private ILogDataService logDataService;

    @Override
    public void insertOperatorLog(LogOperateDetail log) throws OperateLogException {
        if (null == log || null == log.getInstance()) {
            throw new OperateLogException(ExceptionCodes.NOT_ALLOW_NULL_WHEN_INSERT, wrap("", "LOG_OPERATE"));
        }
        LogOperate logOperate = log.getInstance();
        logOperate.setOperateTime(new Date());
        insert(logOperate);
        for (LogDataDetail dataDetail : log.getAllLogData()) {
            LogData logData = dataDetail.getInstance();
            logData.setGuidOperate(logOperate.getGuid());
            logDataService.insert(logData);
            for (LogDataChange logChange: dataDetail.getChanges()) {
                logChange.setGuidData(logData.getGuid());
                logChangeService.insert(logChange);
            }
        }
    }

    @Override
    public Page<LogOperate> queryListByUser(Page<LogOperate> page, Wrapper<LogOperate> wrapper) throws OperateLogException {

        Page<LogOperate> pageQue = new Page<>();

        if (null == wrapper.getEntity()){
            wrapper.orderBy(LogOperate.COLUMN_OPERATE_TIME,false);
            pageQue = selectPage(page,wrapper);
        }else {
            LogOperate logOperate = wrapper.getEntity();

            Wrapper<LogOperate> wrapperQue = new EntityWrapper<>();
            wrapperQue.orderBy(LogOperate.COLUMN_OPERATE_TIME,false);
            if (null != logOperate){
                wrapperQue.eq(LogOperate.COLUMN_USER_ID,logOperate.getUserId());
            }
            pageQue = selectPage(page,wrapperQue);
        }
        return pageQue;
    }
}
